﻿Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.UI.Utilities
Imports DotNetNuke.Common
Imports DotNetNuke.Services.Mail
Imports DotNetNuke.Entities.Host
Imports DotNetNuke.UI.Skins.Skin
Imports DotNetNuke.UI.Skins.Controls.ModuleMessage

Namespace DnnSun.Modules.Recommendation
    Partial Public Class MainView
        Inherits PortalModuleBase
        Implements IActionable

        Private _EmailTemplate As String = String.Empty
        Public Property EmailTemplate()
            Get
                If Not Settings.Item("EmailTemplate") Is Nothing Then
                    _EmailTemplate = Settings.Item("EmailTemplate").ToString
                Else
                    _EmailTemplate = Localization.GetString("DefaultTemplate", LocalResourceFile)
                End If

                Return _EmailTemplate
            End Get
            Set(ByVal value)
                _EmailTemplate = value
            End Set
        End Property

        Private _ShowMessageBox As Boolean
        Public ReadOnly Property ShowMessageBox()
            Get
                _ShowMessageBox = False
                If Not Settings.Item("ShowMessageBox") Is Nothing AndAlso Convert.ToBoolean(Settings.Item("ShowMessageBox")) Then
                    _ShowMessageBox = True
                End If

                Return _ShowMessageBox
            End Get
        End Property


        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Page.IsPostBack = False Then
                SetFormFocus(txtFriendName)
                ClientAPI.RegisterKeyCapture(Me.Parent, Me.btnSubmit, Asc(vbCr))
                'txtFriendName.Attributes.Add("onkeypress", "if (event.keyCode == 13) {document.forms[0]." & btnSendFriend.ClientID & ".click();event.keyCode = 0; return false;}")

                ClientAPI.RegisterClientReference(Me.Page, ClientAPI.ClientNamespaceReferences.dnn)
                ClientAPI.RegisterClientVariable(Me.Page, "Container_ID", Me.ClientID + "_", True)
                RenderControls()
            End If
        End Sub

        Private Sub RenderControls()
            If Request.IsAuthenticated Then
                trYourNameLabel.Visible = False
                trYourNameBox.Visible = False
                trYourEmailLabel.Visible = False
                trYourEmailBox.Visible = False

                YourNameRequired.Enabled = False
                YourEmailRequired.Enabled = False
                YourEmailFormat.Enabled = False
            End If

            FriendMessageRequired.Enabled = ShowMessageBox
            If ShowMessageBox Then
                txtFriendName.Attributes.Add("onchange", "DisplayMessage();")
                trMessageLabel.Visible = True
                trMessageBox.Visible = True
                ClientAPI.RegisterClientVariable(Me.Page, "emailtemplate", Server.HtmlDecode(EmailTemplate), True)
            Else
                txtFriendMessage.Text = Server.HtmlDecode(EmailTemplate)
            End If
        End Sub

        Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
            If Page.IsValid Then
                Dim mailFrom As String = IIf(String.IsNullOrEmpty(Me.txtYourEmail.Text), UserInfo.Email, txtYourEmail.Text)
                Dim yourName As String = IIf(String.IsNullOrEmpty(Me.txtYourName.Text), UserInfo.DisplayName, Server.HtmlEncode(Me.txtYourName.Text))
                Dim mailTo As String = Me.txtFriendsEmail.Text
                Dim builder As New StringBuilder

                builder.Append(txtFriendMessage.Text)
                If Not String.IsNullOrEmpty(Settings.Item("CustomMessage").ToString) Then
                    builder.Append(Server.HtmlDecode(Settings.Item("CustomMessage").ToString))
                End If
                builder.Replace("[FRIENDNAME]", Server.HtmlEncode(Me.txtFriendName.Text))
                builder.Replace("[YOURNAME]", yourName)
                builder.Replace("[YOUREMAIL]", mailFrom)
                builder.Replace("[PAGELINK]", NavigateURL(TabId))

                Dim feebBackMsg As String = Mail.SendMail(PortalSettings.Email, mailTo, "", Localization.GetString("MessageSubject", LocalResourceFile), builder.ToString, "", "HTML", "", "", "", "")
                If String.IsNullOrEmpty(feebBackMsg) Then
                    pnlEmailNuke.Visible = False
                    pnlMsg.Visible = True
                    lblEmailSendMsg.Text = Localization.GetString("MessageSent", LocalResourceFile)
                Else
                    AddModuleMessage(Me, feebBackMsg, ModuleMessageType.RedError)
                End If
            End If
        End Sub

        Public ReadOnly Property ModuleActions() As DotNetNuke.Entities.Modules.Actions.ModuleActionCollection Implements DotNetNuke.Entities.Modules.IActionable.ModuleActions
            Get
                Dim Actions As New Entities.Modules.Actions.ModuleActionCollection
                Actions.Add(GetNextActionID, Localization.GetString("msgModuleOptions", LocalResourceFile), "", Url:=EditUrl("", "", "Module_Options"), Secure:=DotNetNuke.Security.SecurityAccessLevel.Admin, Visible:=True)
                Return Actions
            End Get
        End Property

        Protected Sub btnMore_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnMore.Click
            pnlEmailNuke.Visible = True
            pnlMsg.Visible = False

            'Reset the fileds
            Me.txtFriendName.Text = ""
            Me.txtFriendsEmail.Text = ""
            Me.txtYourEmail.Text = ""
            Me.txtYourName.Text = ""
        End Sub
    End Class
End Namespace
